package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.*;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.SnowflakeUtils;
import com.ruoyi.system.domain.DEqp;
import com.ruoyi.system.domain.DStep;
import com.ruoyi.system.domain.vo.HDResultVo;
import com.ruoyi.system.mapper.DEqpMapper;
import com.ruoyi.system.mapper.DStepMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DResultMapper;
import com.ruoyi.system.domain.DResult;
import com.ruoyi.system.service.IDResultService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 追溯结果Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-16
 */
@Service
public class DResultServiceImpl implements IDResultService 
{
    @Autowired
    private DResultMapper dResultMapper;

    @Autowired
    private DStepMapper dStepMapper;

    @Autowired
    private DEqpMapper dEqpMapper;

    /**
     * 查询追溯结果
     * 
     * @param ID 追溯结果主键
     * @return 追溯结果
     */
    @Override
    public DResult selectDResultByID(Long ID)
    {
        return dResultMapper.selectDResultByID(ID);
    }

    /**
     * 查询追溯结果列表
     * 
     * @param dResult 追溯结果
     * @return 追溯结果
     */
    @Override
    public List<DResult> selectDResultList(DResult dResult)
    {
        return dResultMapper.selectDResultList(dResult);
    }

    /**
     * 新增追溯结果
     * 
     * @param dResult 追溯结果
     * @return 结果
     */
    @Override
    public int insertDResult(DResult dResult)
    {
        return dResultMapper.insertDResult(dResult);
    }

    /**
     * 修改追溯结果
     * 
     * @param dResult 追溯结果
     * @return 结果
     */
    @Override
    public int updateDResult(DResult dResult)
    {
        return dResultMapper.updateDResult(dResult);
    }

    /**
     * 批量删除追溯结果
     * 
     * @param IDs 需要删除的追溯结果主键
     * @return 结果
     */
    @Override
    public int deleteDResultByIDs(Long[] IDs)
    {
        return dResultMapper.deleteDResultByIDs(IDs);
    }

    /**
     * 删除追溯结果信息
     * 
     * @param ID 追溯结果主键
     * @return 结果
     */
    @Override
    public int deleteDResultByID(Long ID)
    {
        return dResultMapper.deleteDResultByID(ID);
    }

    @Override
    public List<HDResultVo> selectResultList_HD(HDResultVo hdResultVo) {
        return dResultMapper.selectResultList_HD(hdResultVo);
    }

    public List<DEqp> getDeqp(DEqp dEqp){
        Long deptId = SecurityUtils.getLoginUser().getDeptId();
        dEqp.setGroupId(deptId.toString());
        return dEqpMapper.selectDEqpList(dEqp);
    }

    public List<DStep> getDStep(DStep dStep){
        Long deptId = SecurityUtils.getLoginUser().getDeptId();
        dStep.setGroupId(deptId.toString());
        return dStepMapper.selectDStepList(dStep);
    }

    @Override
    @Transactional
    public void createResult_HD(HDResultVo hdResultVo) {
        Long deptId = SecurityUtils.getLoginUser().getDeptId();
        hdResultVo.setGroupId(deptId.toString());
        // 若结果表有值则不创建数据
        if (CollectionUtils.isNotEmpty(selectResultList_HD(hdResultVo))) {
            return;
        }
        Random rand = new Random();
        DStep dStepBin = new DStep();
        dStepBin.setStep("BIN");
        List<DStep> dStepBinList = dStepMapper.selectDStepList(dStepBin);
        if (CollectionUtils.isNotEmpty(dStepBinList)) {
            if (null != dStepBinList.get(0).getRunQty()) {
                BigDecimal runQty = dStepBinList.get(0).getRunQty();
                List<Integer> qtyList = new ArrayList<>();
                int sumQty = 0;
                while (sumQty < runQty.intValue()) {
                    int randomNum = rand.nextInt(runQty.intValue()) / 2 + 1;
                    sumQty += randomNum;
                    qtyList.add(randomNum);
                }
                int sumQty1 = 0;
                for (int i = 0; i < qtyList.size() - 1; i++) {
                    sumQty1 += qtyList.get(i);
                }
                qtyList.set(qtyList.size() - 1, runQty.intValue() - sumQty1);
                // 这里的设备号验证先不加
//                DEqp dEqpBin = new DEqp();
//                dEqpBin.setStep("BIN");
//                List<DEqp> dEqpBinList = dEqpMapper.selectDEqpList(dEqpBin);
                // 第一层BIN数据
                DResult dResult = new DResult();
                dResult.setGroupId(deptId.toString());
//                dResult.setID(SnowflakeUtils.nextId());
                dResult.setStep("BIN");
//                dResult.setEqpId(dEqpList.get(rand.nextInt(dEqpList.size())).getEqpId());
                dResult.setEqpId(hdResultVo.getBin_eqp());
                dResult.setEventTime(hdResultVo.getBin_time());
                dResult.setPackName(hdResultVo.getPack_name());
                dResult.setQty(runQty);
                dResultMapper.insertDResult(dResult);
                long rootId = dResult.getID();
                Date rootEventTime = dResult.getEventTime();
                dResult.setPackName(null);
                // 接下来几层
                // 第二层
                DEqp dEqpCs = new DEqp();
                dEqpCs.setStep("CS");
                List<DEqp> dEqpCsList = this.getDeqp(dEqpCs);
                DStep dStepCs = new DStep();
                dStepCs.setStep("CS");
                List<DStep> dStepCsList = this.getDStep(dStepCs);
                // 第三层
                DEqp dEqpBm = new DEqp();
                dEqpBm.setStep("BM");
                List<DEqp> dEqpBmList = this.getDeqp(dEqpBm);
                DStep dStepBm = new DStep();
                dStepBm.setStep("BM");
                List<DStep> dStepBmList = this.getDStep(dStepBm);
                // 第四层
                DEqp dEqpZm = new DEqp();
                dEqpZm.setStep("ZM");
                List<DEqp> dEqpZmList = this.getDeqp(dEqpZm);
                DStep dStepZm = new DStep();
                dStepZm.setStep("ZM");
                List<DStep> dStepZmList = this.getDStep(dStepZm);
                // 第五层
                DEqp dEqpAd = new DEqp();
                dEqpAd.setStep("AD");
                List<DEqp> dEqpAdList = this.getDeqp(dEqpAd);
                DStep dStepAd = new DStep();
                dStepAd.setStep("AD");
                List<DStep> dStepAdList = this.getDStep(dStepAd);
                // 第六层
                DEqp dEqpRc = new DEqp();
                dEqpRc.setStep("RC");
                List<DEqp> dEqpRcList = this.getDeqp(dEqpRc);
                DStep dStepRc = new DStep();
                dStepRc.setStep("RC");
                List<DStep> dStepRcList = this.getDStep(dStepRc);
                // 第七层
                DEqp dEqpLk = new DEqp();
                dEqpLk.setStep("LK");
                List<DEqp> dEqpLkList = this.getDeqp(dEqpLk);
                DStep dStepLk = new DStep();
                dStepLk.setStep("LK");
                List<DStep> dStepLkList = this.getDStep(dStepLk);
                // 第八层
                DEqp dEqpLp = new DEqp();
                dEqpLp.setStep("LP");
                List<DEqp> dEqpLpList = this.getDeqp(dEqpLp);
                DStep dStepLp = new DStep();
                dStepLp.setStep("LP");
                List<DStep> dStepLpList = this.getDStep(dStepLp);
                // 第九层
                DEqp dEqpBsg = new DEqp();
                dEqpBsg.setStep("BSG");
                List<DEqp> dEqpBsgList = this.getDeqp(dEqpBsg);
                DStep dStepBsg = new DStep();
                dStepBsg.setStep("BSG");
                List<DStep> dStepBsgList = this.getDStep(dStepBsg);
//                // 第十层
//                DEqp dEqpHd = new DEqp();
//                dEqpHd.setStep("HD");
//                List<DEqp> dEqpHdList = this.getDeqp(dEqpHd);
//                DStep dStepHd = new DStep();
//                dStepHd.setStep("HD");
//                List<DStep> dStepHdList = dthis.getDStep(dStepHd);
//                // 第十一层
//                DEqp dEqpQd = new DEqp();
//                dEqpQd.setStep("QD");
//                List<DEqp> dEqpQdList = this.getDeqp(dEqpQd);
                DStep dStepQd = new DStep();
                dStepQd.setStep("QD");
                List<DStep> dStepQdList = this.getDStep(dStepQd);

                // 第十二层(前道)
                DEqp dEqpQdBsg = new DEqp();
                dEqpQdBsg.setStep("QD_BSG");
                List<DEqp> dEqpQdBsgList = this.getDeqp(dEqpQdBsg);
                DStep dStepQdBsg = new DStep();
                dStepQdBsg.setStep("QD_BSG");
                List<DStep> dStepQdBsgList = this.getDStep(dStepQdBsg);
                // 第十三层(前道)
                DEqp dEqpHp = new DEqp();
                dEqpHp.setStep("HP");
                List<DEqp> dEqpHpList = this.getDeqp(dEqpHp);
                DStep dStepHp = new DStep();
                dStepHp.setStep("HP");
                List<DStep> dStepHpList = this.getDStep(dStepHp);
                // 第十四层(前道)
                DEqp dEqpQp = new DEqp();
                dEqpQp.setStep("QP");
                List<DEqp> dEqpQpList = this.getDeqp(dEqpQp);
                DStep dStepQp = new DStep();
                dStepQp.setStep("QP");
                List<DStep> dStepQpList = this.getDStep(dStepQp);
                // 第十五层(前道)
                DEqp dEqpZr = new DEqp();
                dEqpZr.setStep("ZR");
                List<DEqp> dEqpZrList = this.getDeqp(dEqpZr);
                DStep dStepZr = new DStep();
                dStepZr.setStep("ZR");
                List<DStep> dStepZrList = this.getDStep(dStepZr);
                // 第十六层(前道)
                DEqp dEqpFl = new DEqp();
                dEqpFl.setStep("FL");
                List<DEqp> dEqpFlList = this.getDeqp(dEqpFl);
                DStep dStepFl = new DStep();
                dStepFl.setStep("FL");
                List<DStep> dStepFlList = this.getDStep(dStepFl);
                for (int i = 0; i < qtyList.size(); i++) {
                    dResult = new DResult();
                    dResult.setGroupId(deptId.toString());
                    // 第二层CS数据及批次生成
                    dResult.setRelationId(rootId);
//                    dResult.setID(SnowflakeUtils.nextId());
                    dResult.setStep("CS");
                    dResult.setEqpId(dEqpCsList.get(rand.nextInt(dEqpCsList.size())).getEqpId());
                    dResult.setWbBatch("WB"+ SnowflakeUtils.nextId() );
                    dResult.setJlBatch("JL"+SnowflakeUtils.nextId() );
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(rootEventTime);
                    calendar.add(Calendar.MINUTE, -(dStepCsList.get(0).getQtime().intValue() + dStepCsList.get(0).getNextStepQtime().intValue() + rand.nextInt(2 * dStepCsList.get(0).getDfloat().intValue()) - dStepCsList.get(0).getDfloat().intValue()));
                    dResult.setEventTime(calendar.getTime());
                    dResult.setQty(new BigDecimal(qtyList.get(i)));
                    // 批次生成规则后面再写
                    dResult.setBatchId(SnowflakeUtils.nextId() + "");
                    dResultMapper.insertDResult(dResult);
                    dResult.setQty(new BigDecimal(0));
                    dResult.setBatchId(null);
                    // 第三层BM数据及批次生成
                    dResult.setRelationId(dResult.getID());
//                    dResult.setID(SnowflakeUtils.nextId());
                    dResult.setStep("BM");
                    dResult.setEqpId(dEqpBmList.get(rand.nextInt(dEqpBmList.size())).getEqpId());
                    calendar = Calendar.getInstance();
                    calendar.setTime(dResult.getEventTime());
                    calendar.add(Calendar.MINUTE, -(dStepBmList.get(0).getQtime().intValue() + dStepBmList.get(0).getNextStepQtime().intValue() + rand.nextInt(2 * dStepBmList.get(0).getDfloat().intValue()) - dStepBmList.get(0).getDfloat().intValue()));
                    dResult.setEventTime(calendar.getTime());
                    dResultMapper.insertDResult(dResult);
                    // 第四层ZM数据及批次生成
                    dResult.setRelationId(dResult.getID());
//                    dResult.setID(SnowflakeUtils.nextId());
                    dResult.setStep("ZM");
                    dResult.setEqpId(dEqpZmList.get(rand.nextInt(dEqpZmList.size())).getEqpId());
                    calendar = Calendar.getInstance();
                    calendar.setTime(dResult.getEventTime());
                    calendar.add(Calendar.MINUTE, -(dStepZmList.get(0).getQtime().intValue() + dStepZmList.get(0).getNextStepQtime().intValue() + rand.nextInt(2 * dStepZmList.get(0).getDfloat().intValue()) - dStepZmList.get(0).getDfloat().intValue()));
                    dResult.setEventTime(calendar.getTime());
                    dResultMapper.insertDResult(dResult);
                    // 第五层AD数据及批次生成
                    dResult.setRelationId(dResult.getID());
//                    dResult.setID(SnowflakeUtils.nextId());
                    dResult.setStep("AD");
                    dResult.setEqpId(dEqpAdList.get(rand.nextInt(dEqpAdList.size())).getEqpId());
                    calendar = Calendar.getInstance();
                    calendar.setTime(dResult.getEventTime());
                    calendar.add(Calendar.MINUTE, -(dStepAdList.get(0).getQtime().intValue() + dStepAdList.get(0).getNextStepQtime().intValue() + rand.nextInt(2 * dStepAdList.get(0).getDfloat().intValue()) - dStepAdList.get(0).getDfloat().intValue()));
                    dResult.setEventTime(calendar.getTime());
                    dResultMapper.insertDResult(dResult);
                    // 第六层RC数据及批次生成
                    dResult.setRelationId(dResult.getID());
//                    dResult.setID(SnowflakeUtils.nextId());
                    dResult.setStep("RC");
                    dResult.setEqpId(dEqpRcList.get(rand.nextInt(dEqpRcList.size())).getEqpId());
                    calendar = Calendar.getInstance();
                    calendar.setTime(dResult.getEventTime());
                    calendar.add(Calendar.MINUTE, -(dStepRcList.get(0).getQtime().intValue() + dStepRcList.get(0).getNextStepQtime().intValue() + rand.nextInt(2 * dStepRcList.get(0).getDfloat().intValue()) - dStepRcList.get(0).getDfloat().intValue()));
                    dResult.setEventTime(calendar.getTime());
                    dResultMapper.insertDResult(dResult);
                    // 第七层LK数据及批次生成
                    dResult.setRelationId(dResult.getID());
//                    dResult.setID(SnowflakeUtils.nextId());
                    dResult.setStep("LK");
                    dResult.setEqpId(dEqpLkList.get(rand.nextInt(dEqpLkList.size())).getEqpId());
                    calendar = Calendar.getInstance();
                    calendar.setTime(dResult.getEventTime());
                    calendar.add(Calendar.MINUTE, -(dStepLkList.get(0).getQtime().intValue() + dStepLkList.get(0).getNextStepQtime().intValue() + rand.nextInt(2 * dStepLkList.get(0).getDfloat().intValue()) - dStepLkList.get(0).getDfloat().intValue()));
                    dResult.setEventTime(calendar.getTime());
                    dResultMapper.insertDResult(dResult);
                    // 第八层LP数据及批次生成
                    dResult.setRelationId(dResult.getID());
//                    dResult.setID(SnowflakeUtils.nextId());
                    dResult.setStep("LP");
                    dResult.setEqpId(dEqpLpList.get(rand.nextInt(dEqpLpList.size())).getEqpId());
                    calendar = Calendar.getInstance();
                    calendar.setTime(dResult.getEventTime());
                    calendar.add(Calendar.MINUTE, -(dStepLpList.get(0).getQtime().intValue() + dStepLpList.get(0).getNextStepQtime().intValue() + rand.nextInt(2 * dStepLpList.get(0).getDfloat().intValue()) - dStepLpList.get(0).getDfloat().intValue()));
                    dResult.setEventTime(calendar.getTime());
                    dResultMapper.insertDResult(dResult);
                    // 第九层BSG数据及批次生成
                    dResult.setRelationId(dResult.getID());
//                    dResult.setID(SnowflakeUtils.nextId());
                    dResult.setStep("BSG");
                    dResult.setEqpId(dEqpBsgList.get(rand.nextInt(dEqpBsgList.size())).getEqpId());
                    calendar = Calendar.getInstance();
                    calendar.setTime(dResult.getEventTime());
                    calendar.add(Calendar.MINUTE, -(dStepBsgList.get(0).getQtime().intValue() + dStepBsgList.get(0).getNextStepQtime().intValue() + rand.nextInt(2 * dStepBsgList.get(0).getDfloat().intValue()) - dStepBsgList.get(0).getDfloat().intValue()));
                    dResult.setEventTime(calendar.getTime());
                    dResultMapper.insertDResult(dResult);
                    // 第十层HD数据及批次生成
                    dResult.setRelationId(dResult.getID());
//                    dResult.setID(SnowflakeUtils.nextId());
                    dResult.setStep("HD");
                    // 批次生成规则后面再写
                    dResult.setBatchId(SnowflakeUtils.nextId() + "");
                    dResultMapper.insertDResult(dResult);
                    // 第十一层QD数据及批次生成
                    dResult.setRelationId(dResult.getID());
//                    dResult.setID(SnowflakeUtils.nextId());
                    dResult.setStep("QD");
                    // 批次生成规则后面再写
                    dResult.setBatchId(SnowflakeUtils.nextId() + "");
                    dResult.setQty(new BigDecimal(dStepQdList.get(0).getLotQty()));
                    dResultMapper.insertDResult(dResult);

                    // 第十二层QD_BSG数据及批次生成(前道)
                    dResult.setRelationId(dResult.getID());
                    dResult.setStep("QD_BSG");
                    dResult.setEqpId(dEqpQdBsgList.get(rand.nextInt(dEqpQdBsgList.size())).getEqpId());
                    calendar = Calendar.getInstance();
                    calendar.setTime(dResult.getEventTime());
                    calendar.add(Calendar.MINUTE, -(dStepQdBsgList.get(0).getQtime().intValue() + dStepQdBsgList.get(0).getNextStepQtime().intValue() + rand.nextInt(2 * dStepQdBsgList.get(0).getDfloat().intValue()) - dStepQdBsgList.get(0).getDfloat().intValue()));
                    dResult.setEventTime(calendar.getTime());
                    dResultMapper.insertDResult(dResult);
                    // 第十三层HP数据及批次生成(前道)
                    dResult.setRelationId(dResult.getID());
                    dResult.setStep("HP");
                    dResult.setEqpId(dEqpHpList.get(rand.nextInt(dEqpHpList.size())).getEqpId());
                    calendar = Calendar.getInstance();
                    calendar.setTime(dResult.getEventTime());
                    calendar.add(Calendar.MINUTE, -(dStepHpList.get(0).getQtime().intValue() + dStepHpList.get(0).getNextStepQtime().intValue() + rand.nextInt(2 * dStepHpList.get(0).getDfloat().intValue()) - dStepHpList.get(0).getDfloat().intValue()));
                    dResult.setEventTime(calendar.getTime());
                    dResultMapper.insertDResult(dResult);
                    // 第十四层QP数据及批次生成(前道)
                    dResult.setRelationId(dResult.getID());
                    dResult.setStep("QP");
                    dResult.setEqpId(dEqpQpList.get(rand.nextInt(dEqpQpList.size())).getEqpId());
                    calendar = Calendar.getInstance();
                    calendar.setTime(dResult.getEventTime());
                    calendar.add(Calendar.MINUTE, -(dStepQpList.get(0).getQtime().intValue() + dStepQpList.get(0).getNextStepQtime().intValue() + rand.nextInt(2 * dStepQpList.get(0).getDfloat().intValue()) - dStepQpList.get(0).getDfloat().intValue()));
                    dResult.setEventTime(calendar.getTime());
                    dResultMapper.insertDResult(dResult);
                    // 第十五层ZR数据及批次生成(前道)
                    dResult.setRelationId(dResult.getID());
                    dResult.setStep("ZR");
                    dResult.setEqpId(dEqpZrList.get(rand.nextInt(dEqpZrList.size())).getEqpId());
                    calendar = Calendar.getInstance();
                    calendar.setTime(dResult.getEventTime());
                    calendar.add(Calendar.MINUTE, -(dStepZrList.get(0).getQtime().intValue() + dStepZrList.get(0).getNextStepQtime().intValue() + rand.nextInt(2 * dStepZrList.get(0).getDfloat().intValue()) - dStepZrList.get(0).getDfloat().intValue()));
                    dResult.setEventTime(calendar.getTime());
                    dResultMapper.insertDResult(dResult);
                    // 第十六层FL数据及批次生成(前道)
                    dResult.setRelationId(dResult.getID());
                    dResult.setStep("FL");
                    dResult.setEqpId(dEqpFlList.get(rand.nextInt(dEqpFlList.size())).getEqpId());
                    calendar = Calendar.getInstance();
                    calendar.setTime(dResult.getEventTime());
                    calendar.add(Calendar.MINUTE, -(dStepFlList.get(0).getQtime().intValue() + dStepFlList.get(0).getNextStepQtime().intValue() + rand.nextInt(2 * dStepFlList.get(0).getDfloat().intValue()) - dStepFlList.get(0).getDfloat().intValue()));
                    dResult.setEventTime(calendar.getTime());
                    // 批次生成规则后面再写
                    dResult.setBatchId(SnowflakeUtils.nextId() + "");
                    dResult.setQty(new BigDecimal(dStepFlList.get(0).getLotQty()).subtract(new BigDecimal(rand.nextInt(dStepFlList.get(0).getFailFloat().intValue()))));
                    dResultMapper.insertDResult(dResult);
                }
            }
        }
    }

}
